package com.tom.architect02.zframework.wms.skin.higher_lib.utils;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;


import com.tom.architect02.R;
import com.tom.architect02.zframework.wms.skin.higher_lib.SkinView;
import com.tom.architect02.zframework.wms.skin.higher_lib.ViewAttr;

import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SkinFactory implements LayoutInflater.Factory2 {

    public static final List<String> mAttrName = new ArrayList<>();

    private List<SkinView> skinViews = new ArrayList<>();

    static {
        mAttrName.add("background");
        mAttrName.add("src");
        mAttrName.add("textColor");
        mAttrName.add("drawableLeft");
        mAttrName.add("drawableTop");
        mAttrName.add("drawableRight");
        mAttrName.add("drawableBottom");
    }

    private static final String[] mClassPrefixList = {
            "android.widget.",
            "android.webkit.",
            "android.app.",
            "android.view."
    };

    private static final Map<String, Constructor<? extends View>> mConstructorMap
            = new HashMap<>();
    private static final Class<?>[] mConstructorSignature = new Class[]{
            Context.class, AttributeSet.class};

    @Nullable
    @Override
    public View onCreateView(@Nullable View parent, @NonNull String name, @NonNull Context context, @NonNull AttributeSet attrs) {
        View view;
        if (-1 == name.indexOf('.')) {
            view = createSDKView(name, context, attrs);
        } else {
            view = onCreateView(name, context, attrs);
        }
        collectView(context, view, attrs);
        return view;
    }

    private View createSDKView(String name, Context context, AttributeSet
            attrs) {
        for (String androidName : mClassPrefixList) {
            return onCreateView(androidName +
                    name, context, attrs);
        }
        return null;
    }

    private Constructor<? extends View> findConstructor(Context context, String name) {
        Constructor<? extends View> constructor = mConstructorMap.get(name);
        if (constructor == null) {
            try {
                Class<? extends View> clazz = context.getClassLoader().loadClass
                        (name).asSubclass(View.class);
                constructor = clazz.getConstructor(mConstructorSignature);
                mConstructorMap.put(name, constructor);
            } catch (Exception e) {
            }
        }
        return constructor;
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull String name, @NonNull Context context, @NonNull AttributeSet attrs) {
        Constructor<? extends View> constructor = findConstructor(context, name);
        try {
            return constructor.newInstance(context, attrs);
        } catch (Exception e) {
        }
        return null;
    }

    public void collectView(Context context, View currentView, AttributeSet attr) {
        List<ViewAttr> viewAttrList = new ArrayList<>();
        TypedArray typedArray = context.obtainStyledAttributes(attr, R.styleable.skin);
        boolean isChange = typedArray.getBoolean(R.styleable.skin_changeSkin, false);
        typedArray.recycle();
        if (!isChange)
            return;
        for (int i = 0; i < attr.getAttributeCount(); i++) {
            String attributeName = attr.getAttributeName(i);
            if (mAttrName.contains(attributeName)) {
                String attributeValue = attr.getAttributeValue(i);
                if (attributeValue.startsWith("#")) {
                    continue;
                }

                int resId = Integer.parseInt(attributeValue.substring(1));
                //这里可不可以直接存储BG
                String resourceName = context.getResources().getResourceEntryName(resId);
                String typeName = context.getResources().getResourceTypeName(resId);
                //这一块的母的，
                ViewAttr viewAttr = new ViewAttr(attributeName, resId);
                viewAttrList.add(viewAttr);
            }
        }
        if (viewAttrList.size() > 0) {
            SkinView skinView = new SkinView(viewAttrList, currentView);
            skinViews.add(skinView); // 收集activity的所有控件。
        }
    }

    public void apply() {
        for (int i = 0; i < skinViews.size(); i++) {
            SkinView skinView = skinViews.get(i);
            skinView.apply();
        }
    }

}
